package com.buer.storage.business.service.impl;


import com.buer.storage.business.dao.ProdDao;
import com.buer.storage.business.domain.Prod;
import com.buer.storage.business.dto.ProdDto;
import com.buer.storage.business.service.ProdService;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.utils.BaseResult;
import com.buer.storage.common.utils.CheckEmptyUtil;
import com.buer.storage.common.utils.OssUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

//装备信息管理
@Service
public class ProdServiceImpl implements ProdService {
    @Resource
    private ProdDao prodDao;

    //增加装备
    @Override
    public int add(ProdDto prodDto) {
        return prodDao.add(prodDto);
    }

    //删除装备
    @Override
    public int delete(int prodId) {
        return prodDao.delete(prodId);
    }

    //修改装备
    @Override
    public int update(Prod prod) {
        return prodDao.update(prod);
    }

    //查询装备
    @Override
    public List<ProdDto> select() {
        return prodDao.select();
    }

    //通过名字或者编号查询
    @Override
    public List<Prod> selectByName(String prodName, String prodCode) {
        return prodDao.selectByName(prodName, prodCode);
    }

    //通过装备分类查询
    @Override
    public List<ProdDto> selectByCategory(int categoryId) {
        List<ProdDto> prod = prodDao.selectByCategory(categoryId);
        return prod;
    }

    //上传图片
    @Override
    public BaseResult upload(MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                String fn = file.getOriginalFilename();
                byte[] data = file.getBytes();

                    String url = OssUtil.upload(fn, data);
                    if (CheckEmptyUtil.isNotEmpty(url)) {
                        return BaseResult.build(ResultEnum.SUCCESS,url);
                    }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }


}
